const app = getApp();
Page({
  data: {
    list: [],
    imgUrl: '',
  },

  onLoad() {
    this.getBook();
    this.setData({
      imgUrl: app.imgUrl
    });
    wx.stopPullDownRefresh()
  },

  getBook() {
    const token = wx.getStorageSync('token');
    wx.request({
      url: app.apiUrl + '/books/user',
      method: "GET",
      header:{
        "token":token
      },
      success: res => {
        console.log(res)
        this.setData({
          list: res.data.data
        });
      }
    });
  },

  // 删除图书方法
  deleteBook(e) {
    const bookId = e.currentTarget.dataset.id;
    const app = getApp();
    wx.showModal({
      title: '确认删除',
      content: '确定要删除此图书吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: `${app.apiUrl}/books/${bookId}`,
            method: 'DELETE',
            data: {  },
            success: (res) => {
              if (res.data.code === 1) {
                wx.showToast({ title: '删除成功', icon: 'success' });
                this.getBook(); // 删除成功后刷新数据
              } else {
                wx.showToast({ title: '删除失败', icon: 'error' });
              }
            }
          });
        }
      }
    });
  },
  onPullDownRefresh(){
    this.onLoad()
},
  // 跳转到图书修改页面
  navitobook(e) {
    const bookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/book/edit/edit?id=${bookId}`, // 假设 edit 页面路径为 `/pages/book/edit/edit`
    });
  }
});
